import 'dart:ffi';

import 'package:development_assistant/model/server_info.dart';
import 'package:development_assistant/service/env_service.dart';
import 'package:development_assistant/service/server_service.dart';
import 'package:get/get.dart';
import 'package:web_socket_channel/web_socket_channel.dart';

class ServerController extends GetxController{

  var isChinese=0.obs;

  var serverID=0.obs;
  var serverType="".obs;
  var serverName="".obs;
  var serverHost="".obs;
  var serverPort="".obs;
  var serverUrl="".obs;
  var linkResult="".obs;
  var linkStatue=0.obs;
  var serverList=RxList<ServerInfo>();

  var envName="".obs;
  var envUrl="".obs;
  var envList=[].obs;

  ServerService serverService=ServerService();
  EnvService envService=EnvService();

  Future link() async{
    try{
      final channel = WebSocketChannel.connect(
          Uri.parse(serverUrl.value)
      );
      channel.stream.listen((event) {
        linkResult.value=event;
        linkStatue.value=200;
        Get.snackbar("连接服务器".tr, "${'成功连接服务器'.tr} : ${serverUrl.value}");
      },onError: (error){
        linkResult.value=error;
        linkStatue.value=400;
        Get.snackbar("连接服务器".tr, "${'连接服务器失败'.tr} : ${serverUrl.value}");
      });
    }catch(e){
      linkResult.value=e.toString();
      linkStatue.value=400;
      Get.snackbar("连接服务器".tr, "${'连接服务器失败'.tr} : ${serverUrl.value}");
    }
  }

  Future getServerList() async{
    List<Map<String,dynamic>> list=await serverService.getServerList();
    serverList.assignAll(list.map((e) => ServerInfo.fromJson(e)).toList());
  }
  
  Future getServerInfo(int? searchID) async{
    List<Map<String,dynamic>> server=await serverService.getServerInfo(searchID);
    ServerInfo serverInfo=ServerInfo.fromJson(server[0]);
    serverID.value=serverInfo.serverID!;
    serverType.value=serverInfo.serverType;
    serverName.value=serverInfo.serverName;
    serverHost.value=serverInfo.serverHost;
    serverPort.value=serverInfo.serverPort;
    serverUrl.value=serverInfo.serverUrl;
  }

  Future addServer() async{
    ServerInfo server= ServerInfo(null,serverType.value, serverName.value, serverHost.value, serverPort.value, serverUrl.value);
    await serverService.addServer(server);
    Get.snackbar("保存服务器信息".tr, "${'成功保存服务器信息'.tr} : ${serverName.value}");
    Get.offNamed("/Home",arguments: {"selectIndex":0});
  }
  
  Future delServer() async{
    await serverService.delServer(serverID.value);
    Get.snackbar("修改服务器信息".tr, "${'成功删除服务器信息'.tr} : ${serverName.value}");
    Get.offNamed("/Home",arguments: {"selectIndex":0});
  }
  
  Future updateServer() async{
    ServerInfo server= ServerInfo(serverID.value,serverType.value, serverName.value, serverHost.value, serverPort.value, serverUrl.value);
    await serverService.updateServer(server);
    Get.snackbar("修改服务器信息".tr, "${'成功修改服务器信息'.tr} : ${serverName.value}");
    Get.offNamed("/Home",arguments: {"selectIndex":0});
  }

  Future setEnv() async{
    Map<String,Object> envMap={
      "env_name":envName.value,
      "env_url":envUrl.value
    };
    envService.setEnv(envMap);
    Get.back();
    Get.snackbar(envName.value.tr, "${envName.value.tr} ${'前置Url'.tr} : ${envUrl.value}");
  }

  Future getEnvInfo(String envType) async{
    var result=await envService.envInfo(envType);
    envUrl.value=result[0]['env_url'];
  }



}